library(tidyverse)
library(wesanderson)
library(MetBrewer)
library(sp)
library(sf)
library(classInt)
library(raster)
library(fixest)
library(rgdal)
library(raster)
library(classInt)

# map of wealth, pm, scatter in corner, and 


############################################# functions
add.alpha <- function(col, alpha=1){
  if(missing(col))
    stop("Please provide a vector of colours.")
  apply(sapply(col, col2rgb)/255, 2, 
        function(x) 
          rgb(x[1], x[2], x[3], alpha=alpha))  
}	
get_grid_raster <- function(raster){
  output <- raster(raster)
  output[] <- 1:ncell(output)
  return(output)
}
identify_non_cells <- function(raster, poly){
  raster = raster(raster)
  raster[] <- 1:ncell(raster)
  r_ext <- extract(raster, poly)
  all_cells <- 1:ncell(raster)
  non_cells <- all_cells[all_cells %in% unlist(r_ext) == F]
  return(non_cells)
  
}


#load shapefiles


lagos <- readOGR("figdat/GIS/lagos_urca.shp")
lagos1 <- readOGR("figdat/GIS/lagos_official.shp")


delhi <- readOGR("figdat/GIS/delhi_urca.shp")
delhi1 <- readOGR("figdat/GIS/delhi_official.shp")


rio <- readOGR("figdat/GIS/rio_urca.shp")
rio1 <- readOGR("figdat/GIS/rio_official.shp")





load("figdat/figSI17_figdata.RData")

  
########################### plot ######################
  
  
  pal_pm <- colorRampPalette(colors = MetBrewer::met.brewer("VanGogh2", 8))(2000) %>% rev()
  pal_elev <- colorRampPalette(colors = MetBrewer::met.brewer("Tiepolo", 8))(2000) %>% rev()
  pal_wealth <- colorRampPalette(colors = c("red3",MetBrewer::met.brewer("Benedictus", 16)[c(1:6, 8:13)], "navy"))(2000) 
  
  
  pdf("figures/figure_SI17.pdf", width = 12, height = 12)                
        par(mfrow = c(3,3))
        par(mar = c(4,2,2,2))
        
        
        pm_rio_plot <- pm_rio
        pm_lag_plot <-  pm_lag
        pm_del_plot <-  pm_del
        
        ### RIO ###
    
        #[1]    
        plot(rio, col = NA, border = NA)
        plot(pm_rio_plot, col = pal_pm, axes = F,add=T, legend = F)
        plot(rio, add = T, lwd =2)

        #[2]
        int <- classIntervals(w_rio$rwi, breaks = 100)
        col_rio_w <- findColours(int, (pal_wealth))
        plot(rio, col = NA, border = NA)
        points(w_rio$longitude, w_rio$latitude, pch = 15, cex = 2.8, col = col_rio_w)
        plot(rio, add = T, lwd =2)
        
        
       
        
        #[3]
         plot(rio, col = NA, border = NA)
        plot(el_rio, col = (pal_elev), axes = F,bty = "n",add=T, legend = F)
        plot(rio, add = T, lwd =2)
        
        
        
        
        
        
        ### Delhi ###
        
        del <- delhi
        #[1]
        plot(del, col = NA, border = NA)
        plot(pm_del_plot, col = pal_pm, axes = F,bty = "n",add=T, legend = F)
        plot(del, add = T, lwd =2)
       
        #[2]
        int <- classIntervals(w_del$rwi, breaks = 100)
        col_del_w <- findColours(int, (pal_wealth))
        plot(del, col = NA, border = NA)
        points(w_del$longitude, w_del$latitude, pch = 15, cex = 5, col = col_del_w)
        plot(del, add = T, lwd =2)
       
        
        #[3]
        
        plot(del, col = NA, border = NA)
        plot(el_del, col = (pal_elev), axes = F,bty = "n",add=T, legend = F)
        plot(del, add = T, lwd =2)
       
        
        ### Lagos ###
        
        
      #[1]  
        plot(lagos, col = NA, border = NA)
        plot(pm_lag_plot, col = pal_pm, axes = F,bty = "n",add=T, legend = F)
        plot(lagos, add = T, lwd =2)
      

        
        #[2]
        int <- classIntervals(w_lag$rwi, breaks = 100)
        col_lag_w <- findColours(int, (pal_wealth))
        
        plot(lagos, col = NA, border = NA)
        points(w_lag$longitude, w_lag$latitude, pch = 15, cex = 3, col = col_lag_w)
        
        plot(lagos, add = T, lwd =2)

        #[3]
        
        
        
        
        plot(lagos, col = NA, border = NA)
        plot(el_lag, col = (pal_elev), axes = F,bty = "n",add=T, legend = F)
        plot(lagos, add = T, lwd =2)
       
        dev.off()
        
        
###############################################################################################################        
                
           #### PLOT LEGEND ####     
                png("figures/figSI17-legend.png", width = 1200, height = 400)                
                
                        ### RIO ###
                        par(mfrow = c(1,3))
                      
                          #[1]    
                        plot(rio, col = NA, border = NA)
                       
                        
                        plotrix::gradient.rect(-43.72,-23.12,-43.21,-23.1,
                                               col=(plotrix::smoothColors(colorRampPalette(pal_pm)(2000))), 
                                               gradient="x", border = 'black')
                        
                        #[2]
                         plot(rio, col = NA, border = NA)
                        
                        plotrix::gradient.rect(-43.72,-23.12,-43.21,-23.1,
                                               col=(plotrix::smoothColors(colorRampPalette(pal_wealth)(2000))), 
                                               gradient="x", border = 'black')
                        
                        
                        #[3]
                        
                        
                        plot(rio, col = NA, border = NA)
                      
                        plotrix::gradient.rect(-43.72,-23.12,-43.21,-23.1,
                                               col=(plotrix::smoothColors(colorRampPalette(pal_elev)(2000))), 
                                               gradient="x", border = 'black')
                        
                    dev.off()    
                
                
                
                
        
